<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="广告标题"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getBanners">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="banners" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="title" label="标题" width="180"  >
            </el-table-column>
            <el-table-column prop="titleUg" label="标题(UG)" width="180"  >
            </el-table-column>
            <el-table-column prop="src" label="图片" width="180">
                <template   slot-scope="scope">
                    <img :src="renderImage(scope.row.src)" height="60" style="margin-top: 3px" />
                </template>
            </el-table-column>
            <el-table-column prop="type" label="位置" width="120" :formatter="positionFormat"  sortable>
            </el-table-column>
            <el-table-column prop="link" width="180" label="链接"  >
            </el-table-column>
            <el-table-column prop="background" label="背景色" width="120" style="vertical-align: center" >
                <template slot-scope="scope" >
                    <div class="bgClass" :style="{'background':scope.row.background}" ></div>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态" min-width="120" sortable :formatter="formatStatus" >
            </el-table-column>
            <el-table-column label="操作" width="320">
                <template scope="scope">
                    <el-button type="warning" v-if="scope.row.status==1" size="small" @click="handleState(scope.$index, scope.row)">禁用</el-button>
                    <el-button type="success" v-if="scope.row.status==0" size="small" @click="handleState(scope.$index, scope.row)">启用</el-button>
                    <el-button type="primary" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-row>
                    <el-col :span="12" >
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="addForm.title" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" >
                        <el-form-item label="标题(Ug)" prop="titleUg">
                            <el-input v-model="addForm.titleUg" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="链接" prop="link">
                    <el-input v-model="addForm.link" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="位置" prop="type" >
                    <template>
                        <el-select v-model="addForm.type" placeholder="请选择">
                            <el-option
                                    v-for="item in positions"
                                    :key="item.value"
                                    :label="item.title"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="图片：" prop="src" >

                    <el-upload
                            :action="uploadUrl"
                            :on-success="onUploadSuccess"
                            list-type="picture"
                    >
                        <i class="el-icon-upload" ></i>
                    </el-upload>

                </el-form-item>
                <el-form-item label="状态" prop="gender" >
                    <el-radio-group v-model="addForm.status">
                        <el-radio class="radio" :label="1" >正常</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="editForm.title" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" >
                        <el-form-item label="标题(Ug)" prop="titleUg">
                            <el-input v-model="editForm.titleUg" auto-complete="off"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="链接" prop="link">
                    <el-input v-model="editForm.link" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="位置" prop="type" >
                    <template>
                        <el-select v-model="editForm.type" placeholder="请选择">
                            <el-option
                                    v-for="item in positions"
                                    :key="item.value"
                                    :label="item.title"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="图片：" prop="src" >

                    <el-upload
                            :action="uploadUrl"
                            :on-success="onUploadSuccess"
                            list-type="picture"
                            :file-list="fileList"
                    >
                        <i class="el-icon-upload" ></i>
                    </el-upload>

                </el-form-item>
                <el-form-item label="状态" prop="gender" >
                    <el-radio-group v-model="editForm.status">
                        <el-radio class="radio" :label="1" >正常</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import {bannerStatue, getBannersData ,deleteBannerApi ,saveBanner} from "../../api/api";

    export default {
        data() {
            return {
                uploadUrl:'/admin/banner/upload',
                filters: {
                    name: ''
                },
                banners: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                fileList: [],
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    id: 0,
                    title: '',
                    titleUg: '',
                    link:'',
                    src: '',
                    type:1,
                    status: 1
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    title: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    title: '',
                    link:'',
                    linkUg:'',
                    src: '',
                    type:1,
                    status: 1
                },

                positions: [
                    {
                        title: '首页轮播',
                        value: 1
                    },
                    {
                        title: '广告',
                        value: 2
                    },
                    {
                        title: '导航',
                        value: 3
                    },
                ]
            }
        },
        methods: {
            renderImage:function(src){
                return this.imageService+src;
            },
            //位置
            positionFormat: function (row, column) {
                return row.type == 1 ? '轮播图' : row.type == 2 ? '广告' :row.type==3?'导航': '未知';
            },
            formatStatus: function(row,column){
               return   row.status == 1 ? '正常' : row.status == 0 ? '已禁用' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getBanners();
            },
            onUploadSuccess(res,file){
                this.addForm.src = res.data.path
                this.editForm.src = res.data.path
            },
            //获取banner列表
            getBanners() {
                let para = {
                    page: this.page,
                    name: this.filters.name
                };
                this.listLoading = true;
                //NProgress.start();
                getBannersData(para).then((res) => {
                    let data = res.data.data;
                    this.banners = data;
                    this.listLoading = false;
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { id: row.id };
                    deleteBannerApi(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getBanners();
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                let banner = Object.assign({}, row);
                console.log(banner,'banner')
                this.editForm.id = banner.id
                this.editForm.src = banner.src
                this.editForm.type = banner.type
                this.editForm.title = banner.title
                this.editForm.titleUg = banner.titleUg
                this.editForm.link = banner.link
                this.editForm.status = banner.status
                this.fileList = [{url:this.imageService + banner.src}]
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    title: '',
                    type: 1,
                    src: ''
                };
            },
            handleState: function(index,row){
                let question = '确定要禁用该广告吗？'
                let params  = {
                    id: row.id,
                    status: 0
                }
                if(row.status==0){
                    question = '确定要启用该广告吗？'
                    params.status = 1;
                }
                this.$confirm(question, '提示', {}).then(() => {
                    this.editLoading = true;
                    bannerStatue(params).then((res) => {
                        if(res.code == 200){
                            this.editLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: params.status==0?'已禁用':'已启用',
                                type: 'success'
                            });
                            // this.$refs['editForm'].resetFields();
                            // this.editFormVisible = false;
                            this.getBanners();
                        }
                    });
                })
            },

            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm);
                            saveBanner(para).then((res) => {
                                if(res.code == 200){
                                    this.editLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '编辑成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getBanners();
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                let _this = this;
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                            saveBanner(para).then((res) => {
                                if(res.code == 200){
                                    this.addLoading = false;
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.getBanners();
                                }else{
                                    _this.$message({
                                        type: 'error',
                                        message: res.msg
                                    })
                                }
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除

            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    /*
                    batchRemoveBanner(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getBanners();
                    });
                     */
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getBanners();
        }
    }

</script>

<style scoped>
    .bgClass{
        width: 40px;
        height: 20px;
    }
</style>
